package com.my.lock.v18;

import java.util.concurrent.atomic.AtomicInteger;

public class Counter {

    private int count;
    private AtomicInteger semaphore = new AtomicInteger();

    public void increment() {
        for (; ; ) {
            if (this.semaphore.compareAndSet(0, 1)) {
                this.count += 1;
                this.semaphore.set(0);
                break;
            }
        }
    }

    @Override
    public String toString() {
        return "Counter{" +
                "count=" + count +
                ", semaphore=" + semaphore +
                '}';
    }
}
